<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/1
 * Time: 9:44
 */

namespace app\api\controller;


use think\Db;

class My extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 消息推送状态
     * @return \think\response\Json
     */
    public function msgSend(){
        $is_send = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_is_send');
        return json(['status' => 200, 'msg' => $is_send]);
    }
    
    /**
     * 设置消息推送状态
     * @return \think\response\Json
     */
    public function setSendStatus(){
        $send_status = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_is_send');
        $status = 1;
        if($send_status == 1){
            $status = 2;
        }
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_is_send', $status);
        if($res){
            return json(['status' => 200, 'msg' => '设置成功']);
        }else{
            return json(['status' => 0, 'msg' => '设置失败']);
        }
    }
    
    /**
     * 用户选择帆船还是赛艇
     * @return \think\response\Json
     * user_type      1 帆船   2 赛艇
     */
    public function chooseType(){
        $user_type = input('user_type');
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_type', $user_type);
        if($res){
            return json(['status' => 200, 'msg' => '选择成功']);
        }else{
            return json(['status' => 0, 'msg' => '选择失败']);
        }
    }
    
    /**
     * 用户基本信息
     * @return \think\response\Json
     */
    public function userInfo(){
        $info = Db::table('up_u_user')
            ->field('user_username, user_image, user_create_id, user_watch')
            ->where('user_id', $this->user_id)
            ->find();
        $info['match_num'] = count($this->myMatch(0, 0, 2));
        // 航海距离
        $sail_distance = Db::table('up_sailboat_drill_distance')
            ->where('dd_user_id', $this->user_id)
            ->sum('dd_distance');
        $info['sail_distance'] = $sail_distance;
            
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 用户主页
     * @return \think\response\Json
     * other_user    如果查看的是其他用户主页，则传other_user
     */
    public function userMsg(){
        $other_user = input('other_user');
        if(isset($other_user)){ // 其他用户主页
            $userid = $other_user;
        }else{
            $userid = $this->user_id;
        }
        $info = Db::table('up_u_user')
            ->join('up_area ua1', 'ua1.district_id = user_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = user_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = user_qu', 'left')
            ->join('up_country_code', 'country_id = user_country_id', 'left')
            ->join('up_club', 'club_id = user_club_id', 'left')
            ->where('user_id', $userid)
            ->field('user_id, user_username, user_image, ua1.district sheng_name, ua2.district shi_name, 
            ua3.district qu_name, country_name, user_gender, club_name, user_intro')
            ->find();
        // 粉丝数
        $info['user_fans_num'] = count($this->myFans($userid, 2));
        // 我的关注
        $info['my_like'] = count($this->myLike($userid, 2));
        // 是否已关注
        $is_like = Db::table('up_like')
            ->where('like_user_id', $this->user_id)
            ->where('like_by_user_id', $userid)
            ->find();
        if($is_like){
            $info['is_like'] = 1; // 已关注
        }else{
            $info['is_like'] = 2; // 未关注
        }
        $already_friend = Db::table('up_friend')
            ->where('f_my_id', $this->user_id)
            ->where('f_friend_id', $other_user)
            ->find();
        if($already_friend){
            $info['is_apply'] = 1; // 已经申请过
        }else{
            $info['is_apply'] = 2; // 没有申请过
        }
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 用户发布
     * @return \think\response\Json
     * other_user     其他人的ID 查看其它用户时传
     * show_type      1 查看用户发布   2 查看该用户关注的人的发布
     * is_publish     1 草稿箱   2 发布    查看其它用户或关注用户发布时传2
     * page           页码
     */
    public function userPublished(){
        $other_user = input('other_user');
        $show_type = input('show_type'); // 1 用户发布   2 关注
        $is_publish = input('is_publish'); // 1 草稿箱  2 发布   查看关注用户时，传2
        if(isset($other_user)){ // 其他用户主页
            $userid = $other_user;
        }else{
            $userid = $this->user_id;
        }
        //********** 用户/关注的风讯
        if($show_type == 2){
            $userid = $this->myLike($userid, 3);
        }
        if(!empty($userid)){
            $infomation = $this->infomation($userid, $show_type, $is_publish);
            if($infomation){
                foreach($infomation as $k => $v){
                    if($v['info_images']){
                        $infomation[$k]['info_images'] = explode(',', trim($v['info_images'], ','));
                    }else{
                        $infomation[$k]['info_images'] = [];
                    }
                }
                return json(['status' => 200, 'msg' => $infomation]);
            }else{
                return json(['status' => 200, 'msg' => []]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 关注/粉丝列表
     * @return \think\response\Json
     * other_user    查看其他用户时传
     * type          1 关注列表   2 粉丝列表
     * page          页码
     */
    public function likeOrFans(){
        $other_user = input('other_user');
        $type = input('type');
        if(isset($other_user)){
            $userid = $other_user;
        }else{
            $userid = $this->user_id;
        }
        if($type == 1){
            $list = $this->myLike($userid, 1);
        }else{
            $list = $this->myFans($userid, 1);
        }
        if($list){
            foreach($list as $k => $v){
                $is_like = Db::table('up_like')
                    ->where('like_user_id', $this->user_id)
                    ->where('like_by_user_id', $v['user_id'])
                    ->find();
                if($is_like){
                    $list[$k]['is_like'] = 1; // 已关注
                }else{
                    $list[$k]['is_like'] = 2; // 未关注
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 添加关注/取消关注
     * @return \think\response\Json
     * other_user    操作对象ID
     */
    public function doLike(){
        $other_user = input('other_user');
        $already_like = Db::table('up_like')
            ->where('like_user_id', $this->user_id)
            ->where('like_by_user_id', $other_user)
            ->find();
        if($already_like){ // 取消关注
            $res = Db::table('up_like')->delete($already_like['like_id']);
            $succ = '取消关注成功';
            $err = '取消关注失败';
        }else{ // 添加关注
            $insert = [
                'like_user_id' => $this->user_id,
                'like_by_user_id' => $other_user,
                'like_time' => $this->create_time
            ];
            $res = Db::table('up_like')->insert($insert);
            $succ = '添加关注成功';
            $err = '添加关注失败';
        }
        if($res){
            return json(['status' => 200, 'msg' => $succ]);
        }else{
            return json(['status' => 0, 'msg' => $err]);
        }
    }
    
    /**
     * 用户详情展示
     * @return \think\response\Json
     */
    public function showUserDetail(){
        $user = Db::table('up_u_user')
            ->join('up_country_code', 'country_id = user_country_id', 'left')
            ->join('up_area ua1', 'ua1.district_id = user_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = user_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = user_qu', 'left')
            ->join('up_club', 'club_id = user_club_id', 'left')
            ->field('user_image, user_username, user_intro, user_gender, country_name, 
            ua1.district sheng_name, ua2.district shi_name, ua3.district qu_name, user_birthday, 
            user_real_name, user_ID_number, user_email, user_call, user_urg_phone, user_club_id, 
            club_name, user_sailing_time, user_country_id, user_sheng, user_shi, user_qu, user_height, 
            user_weight, user_fuzhuang')
            ->where('user_id', $this->user_id)
            ->find();
        return json(['status' => 200, 'msg' => $user]);
    }
    
    /**
     * 编辑用户信息
     * @return \think\response\Json
     * user_image           头像
     * user_username        昵称
     * user_intro           简介
     * user_gender          性别  1 男   2 女
     * user_country_id      国家ID
     * user_sheng           省ID
     * user_shi             市ID
     * user_qu              区ID
     * user_birthday        生日
     * user_real_name       真实姓名
     * user_ID_number       身份证号
     * user_email           邮箱
     * user_call            电话号/手机号
     * user_urg_phone       紧急联系人电话
     * user_club_id         俱乐部ID
     * user_sailing_time    航海时间
     */
    public function userUpdate(){
        $params = input('');
        $update['user_update_time'] = $this->create_time;
        // 头像
        if(isset($params['user_image'])) $update['user_image'] = $params['user_image'];
        // 昵称
        if(isset($params['user_username'])) $update['user_username'] = $params['user_username'];
        // 简介
        if(isset($params['user_intro'])) $update['user_intro'] = $params['user_intro'];
        // 性别
        if(isset($params['user_gender'])) $update['user_gender'] = $params['user_gender'];
        // 国家
        if(isset($params['user_country_id'])){
            $update['user_country_id'] = $params['user_country_id'];
            $update['user_shi'] = null;
        }
        // 市，国家固定为ID
        if(isset($params['user_shi'])){
            $update['user_country_id'] = 1;
            $update['user_shi'] = $params['user_shi'];
        }
        // 出生日期
        if(isset($params['user_birthday'])) $update['user_birthday'] = $params['user_birthday'];
        // 真实姓名
        if(isset($params['user_real_name'])) $update['user_real_name'] = $params['user_real_name'];
        // 身份证号
        if(isset($params['user_ID_number'])) $update['user_ID_number'] = $params['user_ID_number'];
        // 邮箱
        if(isset($params['user_email'])) $update['user_email'] = $params['user_email'];
        // 电话号/手机号
        if(isset($params['user_call'])) $update['user_call'] = $params['user_call'];
        // 紧急联系人电话
        if(isset($params['user_urg_phone'])) $update['user_urg_phone'] = $params['user_urg_phone'];
        // 所属俱乐部
        if(isset($params['user_club_id'])) $update['user_club_id'] = $params['user_club_id'];
        // 航海时间
        if(isset($params['user_sailing_time'])) $update['user_sailing_time'] = $params['user_sailing_time'];
        // 身高
        if(isset($params['user_height'])) $update['user_height'] = $params['user_height'];
        // 体重
        if(isset($params['user_weight'])) $update['user_weight'] = $params['user_weight'];
        // 服装尺码
        if(isset($params['user_fuzhuang'])) $update['user_fuzhuang'] = $params['user_fuzhuang'];
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '编辑成功']);
        }else{
            return json(['status' => 0, 'msg' => '编辑失败']);
        }
    }
    
    /**
     * 国内城市列表
     * @return \think\response\Json
     * keyword     查询城市名称
     */
    public function cityList(){
        $keyword = input('keyword');
        $where['level'] = 2;
        if($keyword){
            $where['district'] = ['like', '%' . $keyword . '%'];
        }
        $list = Db::table('up_area')->where($where)->field('district_id, district')->select();
        return json(['status' => 200, 'msg' => $list]);
    }
    
    /**
     * 国家列表
     * @return \think\response\Json
     * keyword    查询国家名称
     */
    public function countryList(){
        $keyword = input('keyword');
        $where = [];
        if($keyword){
            $where['country_name'] = ['like', '%' . $keyword . '%'];
        }
        $list = Db::table('up_country_code')->where($where)->field('country_id, country_name')->select();
        return json(['status' => 200, 'msg' => $list]);
    }
    
    /**
     * 俱乐部列表
     * @return \think\response\Json
     * keyword    查询的俱乐部名称
     */
    public function clubList(){
        $keyword = input('keyword');
        $where['club_status'] = 1;
        if($keyword){
            $where['club_name'] = ['like', '%' . $keyword . '%'];
        }
        $list = Db::table('up_club')
            ->where($where)
            ->field('club_id, club_name, club_intro, club_image')
            ->order('club_time desc')
            ->select();
        return json(['status' => 200, 'msg' => $list]);
    }
    
    /**
     * 删除风讯
     * @return \think\response\Json
     * info_id     风讯ID
     */
    public function delInformation(){
        $info_id = input('info_id');
        $res = Db::table('up_information')->where('info_id', $info_id)->setField('info_status', 2);
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 删除草稿
     * @return \think\response\Json
     * info_id   风讯ID
     */
    public function delCaoGao(){
        $info_id = input('info_id');
        $res = Db::table('up_information')->delete($info_id);
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 我参加的赛事列表
     * @return \think\response\Json
     * match_status    赛事状态   0 全部  1 未开始   2 进行中   3 已结束
     * match_cate      0 全部   1  帆船    2 赛艇
     */
    public function matchList(){
        $match_status = input('match_status'); // 1 未开始   2 进行中   3 已结束
        $match_cate = input('match_cate'); // 1 帆船    2 赛艇
        $match = $this->myMatch($match_status, $match_cate, 1);
        if($match){
            return json(['status' => 200, 'msg' => $match]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 绑定iBoat Watch
     * @return \think\response\Json
     * watch       智能手表
     */
    public function bandWatch(){
        $watch = input('watch');
        $res = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->update([
                'user_watch' => $watch,
                'user_update_time' => $this->create_time
            ]);
        if($res){
            return json(['status' => 200, 'msg' => '绑定成功']);
        }else{
            return json(['status' => 0, 'msg' => '绑定失败']);
        }
    }
    
    /**
     * 交易记录
     * @return \think\response\Json
     * page    页码
     * order_time
     */
    public function myOrder(){
        $order_time = input('order_time'); // 格式2020-04
        
        $where['order_user_id'] = $this->user_id;
        $where['order_status'] = 2;
        if(isset($order_time) && $order_time != ''){
            $firstDay = date('Y-m-d 00:00:00', strtotime('first day of today', strtotime($order_time)));
            $lastDay = date('Y-m-d 23:59:59', strtotime('last day of today', strtotime($order_time)));
            $where['order_pay_time'] = ['between', [$firstDay, $lastDay]];
        }
        $order = Db::table('up_order')
            ->where($where)
            ->field('order_id, order_price, order_pay_time, order_type, order_cate, order_nickname')
            ->order('order_pay_time desc')
            ->page($this->page, 10)
            ->select();
        if($order){
            return json(['status' => 200, 'msg' => $order]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的订单
     * @return \think\response\Json
     * page    页码
     * order_status    订单状态   1 待付款   2 已付款
     */
    public function myImages(){
        $order_status = input('order_status'); // 1 待付款   2 已付款
        $where['order_user_id'] = $this->user_id;
        $where['order_status'] = $order_status;
        $where['order_cate'] = ['IN', '1,2'];
        $order = Db::table('up_order')
            ->field('order_id, order_num, order_price, order_image_id')
            ->order('order_time desc')
            ->page($this->page, 10)
            ->where($where)
            ->select();
        if($order){
            foreach($order as $k => $v){
                $imgs = Db::table('up_match_images')
                    ->where('mi_id', 'IN', $v['order_image_id'])
                    ->column('mi_url');
                $order[$k]['images'] = $imgs;
            }
            return json(['status' => 200, 'msg' => $order]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 订单图片详情
     * @return \think\response\Json
     * order_id      订单ID
     */
    public function orderMatchImages(){
        $order_id = input('order_id');
        $order = Db::table('up_order')
            ->where('order_id', $order_id)
            ->field('order_image_id, order_price')
            ->find();
        $images = Db::table('up_match_images')
            ->where('mi_id', 'IN', $order['order_image_id'])
            ->field('mi_id, mi_url, mi_name, mi_paishe_name, mi_paishe_time, mi_price')
            ->select();
        $number = count($images); // 共几张照片
        
        return json(['status' => 200, 'msg' => $images, 'number' => $number, 'order' => $order]);
    }
    
    /**
     * 提交照片订单
     * @return \think\response\Json
     * order_id    订单ID
     * order_type   订单类型
     */
    public function commitImage(){
        $order_id = input('order_id');
        $order_type = input('order_type'); // 支付方式 1 微信支付  2 支付宝支付  3 微信代付  4 支付宝代付
        $order_num = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
        $order_update = [
            'order_type' => $order_type,
            'order_update_time' => $this->create_time,
            'order_num' => $order_num
        ];
        $order = Db::table('up_order')
            ->where('order_id', $order_id)
            ->field('order_num, order_price')
            ->find();
        $user = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_username, user_phone')
            ->find();
        if($order_type == 1 || $order_type == 2){ //支付
            $order_update['order_cate'] = 1;
            $order_update['order_another_qrcode'] = null;
            $update['order_nickname'] = $user['user_username'];
            $update['order_phone'] = $user['user_phone'];
        }else{ // 代付
            $order_update['order_cate'] = 2;
            $update['order_nickname'] = null;
            $update['order_phone'] = null;
            if($order_type == 3){ // 微信代付
                $qrcode = $this->WeChatPayReady($order_num, $order['order_price']);
            }else{ // 支付宝代付
                $qrcode = $this->aliOtherPay($order_num, $order['order_price']);
            }
            if($qrcode['status'] == 200){
                $other_qrcode = $this->otherPayQrcode($qrcode['msg'], $this->user_id, $order_num);
                $order_update['order_another_qrcode'] = $other_qrcode;
            }else{
                return json(['status' => 0, 'msg' => $qrcode['msg']]);
            }
        }
        
        $res = Db::table('up_order')->where('order_id', $order_id)->update($order_update);
        if($res){
            $order_info = Db::table('up_order')
                ->field('order_id, order_another_qrcode')
                ->where('order_id', $order_id)
                ->find();
            return json(['status' => 200, 'msg' => $order_info]);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 编辑我的报名方式---方案一（照片、帆船，仅针对支付方式的修改）
     * @return \think\response\Json
     * order_id      订单ID
     * pay_type      支付方式   1 微信支付   2 支付宝支付   3 微信代付   4 支付宝代付
     */
    public function changePayType(){
        $order_id = input('order_id');
        $pay_type = input('pay_type');
        
        $order = Db::table('up_order')
            ->where('order_id', $order_id)
            ->field('order_type, order_num, order_price, order_cate, order_match_id')
            ->find();
        
        $user = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_urg_phone, user_sailing_time, user_username, user_phone')
            ->find();
        
        // ****************************如果是比赛，需要进行判断
        if($order['order_cate'] == 3 || $order['order_cate'] == 4){
            $apply = Db::table('up_match_apply_info')
                ->where('ma_match_id', $order['order_match_id'])
                ->where('ma_user_id', $this->user_id)
                ->field('ma_group')
                ->find();
    
            $match = Db::table('up_match')
                ->where('match_id', $order['order_match_id'])
                ->field('match_cate, match_start_time, match_apply_end_time')
                ->find();
            
            $apply_group = Db::table('up_match_apply_group')
                ->where('mag_match_id', $order['order_match_id'])
                ->where('mag_group_id', $apply['ma_group'])
                ->find();
            
            // 判断该赛事，该组别报名人数
            if($apply_group['mag_apply_num'] <= $apply_group['mag_already_num']){
                return json(['status' => 0, 'msg' => '该赛事组别报名人数已满']);
            }
            
            if(time() > strtotime($match['match_apply_end_time'])){
                return json(['status' => 0, 'msg' => '报名已结束']);
            }
    
            $have = ['apply' => 1, 'user_name' => '']; // 用于判断某个用户有没有冲突的赛事
            $have_this = ['apply' => 1, 'user_name' => '']; // 用于判断某个用户有没有冲突的赛事
            $join_users = Db::table('up_match_apply_info')
                ->where('ma_order_num', $order['order_num'])
                ->field('ma_user_id, ma_id, ma_user_name')
                ->select();
            foreach($join_users as $k => $v){
                // 其他比赛
                $otherMatch = Db::table('up_match_apply_info')
                    ->join('up_match', 'match_id = ma_match_id', 'left')
                    ->where('ma_user_id', $v['ma_user_id'])
//                    ->where('match_end_time', '>', $match['match_start_time'])
                    ->where('match_status', 'IN', '1,2')
                    ->where('match_id', 'NEQ', $order['order_match_id'])
                    ->where('ma_apply_status', 2)
                    ->field('match_id')
                    ->find();
                if($otherMatch){
                    $have['apply'] = 2;
                    $have['user_name'] = $v['ma_user_name'];
                }
                // 本场比赛
                $thisMatch = Db::table('up_match_apply_info')
                    ->join('up_match', 'match_id = ma_match_id', 'left')
                    ->where('ma_user_id', $v['ma_user_id'])
                    ->where('match_id', $order['order_match_id'])
                    ->where('ma_id', 'NEQ', $v['ma_id'])
                    ->where('ma_apply_status', 2)
                    ->field('match_id')
                    ->find();
                if($thisMatch){
                    $have_this['apply'] = 2;
                    $have_this['user_name'] = $v['ma_user_name'];
                }
            }
            if($have['apply'] == 2){
                return json(['status' => 0, 'msg' => '用户' . $have['user_name'] . '已报名其他赛事']);
            }
            if($have_this['apply'] == 2){
                return json(['status' => 0, 'msg' => '用户' . $have_this['user_name'] . '已报名本场比赛']);
            }
        }
        //********************************* 帆船比赛判断 end
        
        $update['order_type'] = $pay_type;
        $update['order_update_time'] = $this->create_time;
        $order_num = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
        $update['order_num'] = $order_num;
        if($pay_type == 1 || $pay_type == 2){ //支付
            if($order['order_cate'] == 1 || $order['order_cate'] == 2){ // 照片
                $update['order_cate'] = 1;
            }elseif($order['order_cate'] == 3 || $order['order_cate'] == 4){ // 赛事
                $update['order_cate'] = 3;
            }else{
                $update['order_cate'] = 5;
            }
            
            $update['order_nickname'] = $user['user_username'];
            $update['order_phone'] = $user['user_phone'];
            $update['order_another_qrcode'] = null;
        }else{ // 代付
            if($order['order_cate'] == 1 || $order['order_cate'] == 2){ // 照片
                $update['order_cate'] = 2;
            }elseif($order['order_cate'] == 3 || $order['order_cate'] == 4){ // 赛事
                $update['order_cate'] = 4;
            }else{
                $update['order_cate'] = 6;
            }
            
            $update['order_nickname'] = null;
            $update['order_phone'] = null;
            if($pay_type == 3){ // 微信代付
                $qrcode = $this->WeChatPayReady($order_num, $order['order_price']);
            }else{ // 支付宝代付
                $qrcode = $this->aliOtherPay($order_num, $order['order_price']);
//                try{
//                    $qrcode = $this->aliOtherPay($order_num, $order['order_price']);
//                }catch(\Exception $e){
//                    return json(['status' => 0, 'msg' => $e->getMessage()]);
//                }
            }
            if($qrcode['status'] == 200){
                $other_qrcode = $this->otherPayQrcode($qrcode['msg'], $this->user_id, $order_num);
                $update['order_another_qrcode'] = $other_qrcode;
            }else{
                return json(['status' => 0, 'msg' => $qrcode['msg']]);
            }
        }
        Db::startTrans();
        try{
            // 订单信息
            Db::table('up_order')->where('order_id', $order_id)->update($update);
            // 报名信息
            Db::table('up_match_apply_info')->where('ma_order_num', $order['order_num'])->update(['ma_order_num' => $order_num]);
            Db::commit();
            $res = 1;
        }catch(\Exception $e){
            Db::rollback();
            $res = 2;
        }
        if($res == 1){
            $order_info = Db::table('up_order')
                ->field('order_id, order_another_qrcode')
                ->where('order_id', $order_id)
                ->find();
            return json(['status' => 200, 'msg' => $order_info]);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 提交订单----方案二（仅限帆船，整体报名信息都可以修改）
     * @return \think\response\Json
     */
    public function commitOrder(){
        //************************************ 赛事
        $params = input('apply');
        $params = json_decode($params, true);

//        $match_id = $params['match_id']; // 赛事ID
        $ma_total_price = $params['ma_total_price']; // 总费用
        $ma_cost_info_id = trim($params['ma_cost_info_id'], ','); // 费用明细ID组成的字符串 逗号隔开
        $ma_club_id = $params['ma_club_id']; // 俱乐部ID
        $ma_lingd = $params['ma_lingd']; // 领队
        $ma_coach = $params['ma_coach']; // 教练
        $ma_group = $params['ma_group']; // 组别ID   取mag_group_id
        $ma_fan_code = $params['ma_fan_code']; // 帆号
        $ma_boat_code = $params['ma_boat_code']; // 船号
        $sailor = $params['sailor']; // 船员
        
        //**********************************订单
        $order_type = $params['order_type']; // 订单类型   1 微信支付   2 支付宝支付   3 微信代付   4 支付宝代付
        $order_id = $params['order_id']; // 订单ID
        
        $order = Db::table('up_order')
            ->where('order_id', $order_id)
            ->field('order_num, order_match_id')
            ->find();
        
        $user = Db::table('up_match_apply_info')
            ->where('user_id', $this->user_id)
            ->where('ma_match_id', $order['order_match_id'])
            ->field('ma_id')
            ->find();
    
        $user_info = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_username, user_phone')
            ->find();
        
        $match = Db::table('up_match')
            ->where('match_id', $order['order_match_id'])
            ->field('match_cate, match_start_time')
            ->find();
        
        // 删除之前报名的所有船员
        $del = Db::table('up_match_apply_info')
            ->where('ma_order_num', $order['order_num'])
            ->where('ma_role', 2)
            ->delete();
        if($del){
            // 判断该赛事，该组别报名人数
            $apply_info = Db::table('up_match_apply_group')
                ->where('mag_match_id', $order['order_match_id'])
                ->where('mag_group_id', $ma_group)
                ->find();
            if($apply_info['mag_apply_num'] <= $apply_info['mag_already_num']){
                return json(['status' => 0, 'msg' => '该赛事组别报名人数已满']);
            }
            
            // 报名人信息
            $apply = [
                'ma_total_price' => $ma_total_price,
                'ma_cost_info_id' => $ma_cost_info_id,
                'ma_club_id' => $ma_club_id,
                'ma_lingd' => $ma_lingd,
                'ma_coach' => $ma_coach,
                'ma_group' => $ma_group,
                'ma_fan_code' => $ma_fan_code,
                'ma_boat_code' => $ma_boat_code,
                'ma_time' => $this->create_time,
                'ma_date' => date('Y-m-d')
            ];
            
            // 船员
            $chuany = [];
            $have = ['apply' => 1, 'user_name' => '']; // 用于判断某个用户有没有冲突的赛事
            $have_this = ['apply' => 1, 'user_name' => '']; // 用于判断某个用户有没有冲突的赛事
            if(is_array($sailor) && !empty($sailor)){
                foreach($sailor as $k => $v){
                    $chuany[$k] = [
                        'ma_user_id' => $v['sailor_user_id'],
                        'ma_match_id' => $order['order_match_id'],
                        'ma_total_price' => $ma_total_price,
                        'ma_cost_info_id' => $ma_cost_info_id,
                        'ma_club_id' => $ma_club_id,
                        'ma_lingd' => $ma_lingd,
                        'ma_coach' => $ma_coach,
                        'ma_group' => $ma_group,
                        'ma_fan_code' => $ma_fan_code,
                        'ma_boat_code' => $ma_boat_code,
                        'ma_role' => 2,
                        'ma_time' => $this->create_time,
                        'ma_date' => date('Y-m-d'),
                        'ma_match_cate' => $match['match_cate'],
                        'ma_user_name' => $v['sailor_user_name'],
                        'ma_user_gender' => $v['sailor_user_gender'],
                        'ma_user_birthday' => $v['sailor_user_birthday'],
                        'ma_user_ID_number' => $v['sailor_user_ID_number'],
                        'ma_user_call' => $v['sailor_user_call'],
                        'ma_user_email' => $v['sailor_user_email'],
                        'ma_user_urg_phone' => $v['sailor_user_urg_phone'],
                        'ma_user_sailing_time' => $v['sailor_user_sailing_time'],
                        'ma_real_pay_user_id' => $this->user_id,
                        'ma_order_num' => $order['order_num']
                    ];
                    // 其他比赛
                    $otherMatch = Db::table('up_match_apply_info')
                        ->join('up_match', 'match_id = ma_match_id', 'left')
                        ->where('ma_user_id', $v['sailor_user_id'])
//                        ->where('match_end_time', '>', $match['match_start_time'])
                        ->where('match_status', 'IN', '1,2')
                        ->where('match_id', 'NEQ', $order['order_match_id'])
                        ->where('ma_apply_status', 2)
                        ->field('match_id')
                        ->find();
                    if($otherMatch){
                        $have['apply'] = 2;
                        $have['user_name'] = $v['sailor_user_name'];
                    }
                    // 本场比赛
                    $thisMatch = Db::table('up_match_apply_info')
                        ->join('up_match', 'match_id = ma_match_id', 'left')
                        ->where('ma_user_id', $v['sailor_user_id'])
                        ->where('match_id', $order['order_match_id'])
                        ->where('ma_apply_status', 2)
                        ->field('match_id')
                        ->find();
                    if($thisMatch){
                        $have_this['apply'] = 2;
                        $have_this['user_name'] = $v['sailor_user_name'];
                    }
                }
            }
            if($have['apply'] == 2){
                return json(['status' => 0, 'msg' => '用户' . $have['user_name'] . '已报名其他赛事']);
            }
            if($have_this['apply'] == 2){
                return json(['status' => 0, 'msg' => '用户' . $have_this['user_name'] . '已报名本场比赛']);
            }
            
            // 订单
            $order_update = [
                'order_price' => $ma_total_price,
                'order_update_time' => $this->create_time,
                'order_type' => $order_type
            ];
            if($order_type == 1 || $order_type == 2){ //支付
                $order_update['order_cate'] = 3;
                $order_update['order_nickname'] = $user_info['user_username'];
                $order_update['order_phone'] = $user_info['user_phone'];
                $order_update['order_another_qrcode'] = null;
            }else{ // 代付
                $order_update['order_cate'] = 4;
                $order_update['order_nickname'] = null;
                $order_update['order_phone'] = null;
                if($order_type == 3){ // 微信代付
                    $qrcode = $this->WeChatPayReady($order['order_num'], $ma_total_price);
                }else{ // 支付宝代付
                    $qrcode = $this->aliOtherPay($order['order_num'], $ma_total_price);
                }
                
                if($qrcode['status'] == 200){
                    $other_qrcode = $this->otherPayQrcode($qrcode['msg'], $this->user_id, $order['order_num']);
                    $order_update['order_another_qrcode'] = $other_qrcode;
                }else{
                    return json(['status' => 0, 'msg' => $qrcode['msg']]);
                }
            }
            Db::startTrans();
            try{
                Db::table('up_match_apply_info')->where('ma_id', $user['ma_id'])->update($apply);
                if(!empty($chuany)){
                    Db::table('up_match_apply_info')->insertAll($chuany);
                }
                Db::table('up_order')->where('order_id', $order_id)->update($order_update);
                Db::commit();
                $order_info = Db::table('up_order')
                    ->field('order_id, order_another_qrcode')
                    ->where('order_id', $order_id)
                    ->find();
                return json(['status' => 200, 'msg' => $order_info]);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => '提交失败']);
            }
        }else{
            return json(['status' => 0, 'msg' => '系统异常']);
        }
    }
    
    /**
     * 编辑我的报名方式------赛艇
     * @return \think\response\Json
     * order_id    订单ID
     * pay_type    订单类型  1 微信支付   2 支付宝支付   3 微信代付   4 支付宝代付
     */
    public function changeRowingPayType(){
        $order_id = input('order_id');
        $pay_type = input('pay_type'); // 1 微信支付   2 支付宝支付   3 微信代付   4 支付宝代付
        
        $order = Db::table('up_order')
            ->field('order_num, order_match_id, order_price')
            ->where('order_id', $order_id)
            ->find();
        $match = Db::table('up_match')
            ->field('match_apply_end_time, match_start_time')
            ->where('match_id', $order['order_match_id'])
            ->find();
        $user = Db::table('up_u_user')
            ->field('user_username, user_phone')
            ->where('user_id', $this->user_id)
            ->find();
        
        // ************ 报名结束
        if(time() > strtotime($match['match_apply_end_time'])){
            return json(['status' => 0, 'msg' => '报名已结束']);
        }
        
         // ************** 报名组是否已满
        $apply_group = Db::table('up_match_apply_info')
            ->where('ma_order_num', $order['order_num'])
            ->field('ma_group, count(ma_group) num')
            ->group('ma_group')
            ->select();
        $group_man = ['is_man' => 1, 'group_name' => '']; // 判断某个组是否报满
        foreach($apply_group as $k => $v){
            $group = Db::table('up_match_apply_group')
                ->join('up_match_group', 'mag_group_id = mg_id', 'left')
                ->where('mag_match_id', $order['order_match_id'])
                ->where('mag_group_id', $v['ma_group'])
                ->field('mag_apply_num, mag_already_num, mg_name')
                ->find();
            $num = $v['num'] + $group['mag_already_num'];
            if($num > $group['mag_apply_num']){
                $group_man['is_man'] = 2;
                $group_man['group_name'] = $v['mg_name'];
            }
        }
        if($group_man['is_man'] == 2){
            return json(['status' => 0, 'msg' => $group_man['group_name'] . '报名人数超限']);
        }
        
        // ************ 报名赛事冲突
        $apply_user = Db::table('up_match_apply_info')
            ->where('ma_order_num', $order['order_num'])
            ->field('ma_match_id, ma_user_id, ma_user_name')
            ->group('ma_user_id')
            ->select();
        $other_match = ['is_other' => 1, 'user_name' => '']; // 判断是否和其他赛事冲突
        foreach($apply_user as $kk => $vv){
            $have = Db::table('up_match_apply_info')
//                ->where('match_end_time', '>', $match['match_start_time'])
                ->where('match_status', 'IN', '1,2')
                ->where('ma_user_id', $vv['ma_user_id'])
                ->where('ma_match_id', 'NEQ', $vv['ma_match_id'])
                ->where('ma_apply_status', 2)
                ->find();
            if($have){
                $other_match['is_other'] = 2;
                $other_match['user_name'] = $vv['ma_user_name'];
            }
        }
        if($other_match['is_other'] == 2){
            return json(['status' => 0, 'msg' => $other_match['user_name'] . '用户有其他冲突赛事']);
        }
        //********************   判断  end
        
        $update['order_type'] = $pay_type;
        $update['order_update_time'] = $this->create_time;
        if($pay_type == 1 || $pay_type == 2){ //支付
            $update['order_cate'] = 3;
            $update['order_nickname'] = $user['user_username'];
            $update['order_phone'] = $user['user_phone'];
            $update['order_another_qrcode'] = null;
        }else{ // 代付
            $update['order_cate'] = 4;
            $update['order_nickname'] = null;
            $update['order_phone'] = null;
            if($pay_type == 3){ // 微信代付
                $qrcode = $this->WeChatPayReady($order['order_num'], $order['order_price']);
            }else{ // 支付宝代付
                $qrcode = $this->aliOtherPay($order['order_num'], $order['order_price']);
            }
            if($qrcode['status'] == 200){
                $other_qrcode = $this->otherPayQrcode($qrcode['msg'], $this->user_id, $order['order_num']);
                $update['order_another_qrcode'] = $other_qrcode;
            }else{
                return json(['status' => 0, 'msg' => $qrcode['msg']]);
            }
        }
        $res = Db::table('up_order')->where('order_id', $order_id)->update($update);
        if($res){
            $order_info = Db::table('up_order')
                ->field('order_id, order_another_qrcode')
                ->where('order_id', $order_id)
                ->find();
            return json(['status' => 200, 'msg' => $order_info]);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 我的报名
     * @return \think\response\Json
     * page    页码
     * match_cate      1 帆船   2 赛艇
     */
    public function myOrderMatch(){
        $match_cate = input('match_cate'); // 1 帆船   2 赛艇
        $where['order_user_id'] = $this->user_id;
        $where['order_status'] = ['IN', '1,2'];
        $where['order_cate'] = ['IN', '3,4'];
        $where['match_cate'] = $match_cate;
        $order = Db::table('up_order')
            ->join('up_match', 'match_id = order_match_id', 'left')
            ->join('up_area', 'district_id = match_shi', 'left')
            ->field('order_id, order_num, match_id, match_title, match_image, district, match_country_name, 
            order_price, match_cate, match_status, order_status')
            ->order('order_time desc')
            ->page($this->page, 10)
            ->where($where)
            ->select();
        if($order){
            return json(['status' => 200, 'msg' => $order]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 帆船报名船员回显
     * @return \think\response\Json
     * order_id    订单ID
     */
    public function applySailingUser(){
        $order_id = input('order_id');
        $order = Db::table('up_order')
            ->where('order_id', $order_id)
            ->field('order_num')
            ->find();
        $user = Db::table('up_match_apply_info')
            ->join('up_u_user', 'user_id = ma_user_id', 'left')
            ->where('ma_order_num', $order['order_num'])
            ->where('ma_role', 2)
            ->field('user_id, user_phone')
            ->select();
        if($user){
            return json(['status' => 200, 'msg' => $user]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 添加收藏赛事/取消收藏赛事
     * @return \think\response\Json
     * match_id    赛事ID
     */
    public function doCollect(){
        $match_id = input('match_id');
        $collect = Db::table('up_my_match')
            ->where('mm_user_id', $this->user_id)
            ->where('mm_match_id', $match_id)
            ->find();
        if($collect){
            $res = Db::table('up_my_match')
                ->where('mm_user_id', $this->user_id)
                ->where('mm_match_id', $match_id)
                ->delete();
        }else{
            $insert = [
                'mm_user_id' => $this->user_id,
                'mm_match_id' => $match_id,
                'mm_time' => $this->create_time
            ];
            $res = Db::table('up_my_match')->insert($insert);
        }
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 赛事文件
     * @return \think\response\Json
     * match_id    赛事ID
     */
    public function matchFiles(){
        $match_id = input('match_id');
        $files = Db::table('up_match_files')
            ->where('mf_match_id', $match_id)
            ->field('mf_id, mf_match_id, mf_match_file_url, mf_match_file_name, mf_match_file_bmp, 
            mf_match_file_intro')
            ->order('mf_time desc')
            ->select();
        if($files){
            return json(['status' => 200, 'msg' => $files]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 是否已经实名认证
     * @return \think\response\Json
     */
    public function isSM(){
        $user_real_check = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_real_check');
        return json(['status' => 200, 'msg' => $user_real_check]);
    }
    
    /**
     * 实名认证
     * @return \think\response\Json
     * user_ID_number         身份证号
     * user_real_name         真实姓名
     * user_gender            性别   1 男   2 女
     * user_country_id        国家ID
     * user_sheng             省ID
     * user_shi               市ID
     * user_qu                区ID
     * user_birthday          出生日期
     * user_email             邮箱
     * user_call              电话号/手机号
     * user_urg_phone         紧急联系人手机号
     * user_club_id           俱乐部ID
     * user_sailing_time      航海时间
     */
    public function realName(){
        $params = input('');
        $data = [
            'user_ID_number' => $params['user_ID_number'],
            'user_real_name' => $params['user_real_name'],
            'user_gender' => $params['user_gender'],
//            'user_country_id' => $params['user_country_id'],
//            'user_sheng' => $params['user_sheng'],
//            'user_shi' => $params['user_shi'],
//            'user_qu' => $params['user_qu'],
            'user_birthday' => $params['user_birthday'],
            'user_email' => $params['user_email'],
            'user_call' => $params['user_call'],
            'user_urg_phone' => $params['user_urg_phone'],
            'user_club_id' => $params['user_club_id'],
            'user_sailing_time' => $params['user_sailing_time'],
            'user_real_name_time' => $this->create_time,
            'user_real_check' => 2
        ];
        if(isset($params['user_shi']) && $params['user_shi'] != ''){
            $data['user_shi'] = $params['user_shi'];
            $data['user_country_id'] = 1;
        }
        if(isset($params['user_country_id']) && $params['user_country_id'] != ''){
            $data['user_shi'] = null;
            $data['user_country_id'] = $params['user_country_id'];
        }
        if(isset($params['user_fanlian_ID']) && $params['user_fanlian_ID'] != ''){
            $data['user_fanlian_ID'] = $params['user_fanlian_ID'];
        }
        if(isset($params['user_fanxie_ID']) && $params['user_fanxie_ID'] != ''){
            $data['user_fanxie_ID'] = $params['user_fanxie_ID'];
        }
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($data);
        if($res){
            return json(['status' => 200, 'msg' => '认证成功']);
        }else{
            return json(['status' => 0, 'msg' => '认证失败']);
        }
    }
    
    /**
     * 实名认证回显
     * @return \think\response\Json
     */
    public function showShiMing(){
        $shiming = Db::table('up_u_user')
            ->join('up_area', 'user_shi = district_id', 'left')
            ->join('up_club', 'user_club_id = club_id', 'left')
            ->join('up_country_code', 'country_id = user_country_id', 'left')
            ->where('user_id', $this->user_id)
            ->field('user_ID_number, user_real_name, district, user_birthday, user_email, user_call, 
            user_urg_phone, user_sailing_time, club_name, user_gender, user_club_id, user_country_id, 
            user_shi, country_name, user_fanlian_ID, user_fanxie_ID')
            ->find();
        return json(['status' => 200, 'msg' => $shiming]);
    }
    
    /**
     * 绑定第三方
     * @return \think\response\Json
     * third_type       绑定类型
     * third_token      第三方token
     * third_img        第三方头像
     * third_nickname   第三方昵称
     */
    public function bandThird(){
        $third_type = input('third_type'); // 1 apple   2 微信   3 QQ
        $third_token = input('third_token');
        $third_img = input('third_img');
        $third_nickname = input('third_nickname');
        if($third_type == 1){
            $update = [
                'user_apple_token' => $third_token
            ];
        }elseif($third_type == 2){
            $update = [
                'user_wx_token' => $third_token,
                'user_wx_img' => $third_img,
                'user_wx_nickname' => $third_nickname
            ];
        }else{
            $update = [
                'user_qq_token' => $third_token,
                'user_qq_img' => $third_img,
                'user_qq_nickname' => $third_nickname
            ];
        }
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '绑定成功']);
        }else{
            return json(['status' => 0, 'msg' => '绑定失败']);
        }
    }
    
    /**
     * 解绑第三方
     * @return \think\response\Json
     * third_type     类型  1 apple  2 微信  3 QQ
     */
    public function relieve(){
        $third_type = input('third_type'); // 1 apple   2 微信   3 QQ
        if($third_type == 1){
            $data = [
                'user_apple_token' => null
            ];
        }elseif($third_type == 2){
            $data = [
                'user_wx_token' => null,
                'user_wx_img' => null,
                'user_wx_nickname' => null
            ];
        }else{
            $data = [
                'user_qq_token' => null,
                'user_qq_img' => null,
                'user_qq_nickname' => null
            ];
        }
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($data);
        if($res){
            return json(['status' => 200, 'msg' => '解绑成功']);
        }else{
            return json(['status' => 0, 'msg' => '解绑失败']);
        }
    }
    
    /**
     * 哪些第三方绑定了
     * @return \think\response\Json
     */
    public function thirdStatus(){
        $third = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_apple_token, user_qq_token, user_wx_token')
            ->find();
        return json(['status' => 200, 'msg' => $third]);
    }
    
    /**
     * 注销须知
     * @return \think\response\Json
     */
    public function logoutExplain(){
        $explain = Db::table('up_other')->where('other_id', 3)->value('other_logout');
        return json(['status' => 200, 'msg' => $explain]);
    }
    
    /**
     * 验证中心
     * @return \think\response\Json
     * phone     手机号
     * code      验证码
     */
    public function checkCenter(){
        $phone = input('phone');
        $code = input('code');
        $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($user)) return json(['status' => 0, 'msg' => '该账号未注册']);
        // 验证码
        $yzm = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        // 10分钟超时
        $now = time();
        if($now - $yzm['time'] >= 1801) return json(['status' => 0, 'msg' => '验证码失效']);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
        return json(['status' => 200, 'msg' => true]);
    }
    
    /**
     * 注销原因列表
     * @return \think\response\Json
     */
    public function logoutReason(){
        $list = Db::table('up_logout_reason')->field('reason_id, reason_name')->select();
        return json(['status' => 200, 'msg' => $list]);
    }
    
    /**
     * 提交注销申请
     * @return \think\response\Json
     * reason_id     注销原因ID    其它传0
     * reason        注销原因
     */
    public function logoutApply(){
        $reason_id = input('reason_id');
        $reason = input('reason');
        if($reason_id != 0){
            $reason = Db::table('up_logout_reason')->where('reason_id', $reason_id)->value('reason_name');
        }
        
        // 是否是有效状态
        $user_status = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_status');
        if($user_status != 1) return json(['status' => 0, 'msg' => '检测到您“账号为非有效状态”']);
        // 未完成订单
        $pay_over = Db::table('up_order')
            ->where('order_user_id', $this->user_id)
            ->where('order_status', 1)
            ->find();
        if($pay_over) return json(['status' => 0, 'msg' => '检测到您“账号内有未完成的状态订单”']);
        //  账号内有无纠纷
        $report = Db::table('up_report')
            ->where('report_status', 1)
            ->where(function ($query){
                $query->where('report_user_id', $this->user_id)
                    ->whereOr('report_to_user_id', $this->user_id);
            })
            ->find();
        if($report) return json(['status' => 0, 'msg' => '检测到您“账号内有纠纷”']);
        // 检测账号权限
        $match_ids = Db::table('up_match')
            ->where('match_status', 'IN', '1,2')
            ->column('match_id');
        $houtai = Db::table('up_committee_member')
            ->where('cm_match_id', 'IN', $match_ids)
            ->where('cm_match_user_id', $this->user_id)
            ->find();
        if($houtai) return json(['status' => 0, 'msg' => '该账号为后台指定工作人员账号，不可以注销']);
    
        $apply = [
            'apply_reason_id' => $reason_id,
            'apply_reason' => $reason,
            'apply_time' => $this->create_time
        ];
        $user = Db::table('up_u_user')
            ->field('user_username, user_phone, user_gender, user_email, user_urg_phone')
            ->where('user_id', $this->user_id)
            ->find();
        $apply['apply_user_name'] = $user['user_username'];
        $apply['apply_user_phone'] = $user['user_phone'];
        $apply['apply_user_gender'] = $user['user_gender'];
        $apply['apply_user_email'] = $user['user_email'];
        $apply['apply_user_urg_phone'] = $user['user_urg_phone'];
        
        try{
            // 注销申请表
            Db::table('up_logout_apply')->insert($apply);
            // 修改注销用户信息
            Db::table('up_u_user')
                ->where('user_id', $this->user_id)
                ->update(['user_status' => 4]);
            Db::commit();
            return json(['status' => 200, 'msg' => '注销成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '服务器异常，请稍后重试']);
        }
    }
    
    /**
     * 审核状态
     * @return \think\response\Json
     */
    public function logoutStatus(){
        $status = Db::table('up_logout_apply')
            ->field('apply_status, apply_refuse_reason')
            ->where('apply_user_id', $this->user_id)
            ->find();
        return json(['status' => 200, 'msg' => $status]);
    }
    
    /**
     * 电话、邮箱、网址、app描述、一句话介绍
     * @return \think\response\Json
     */
    public function aboutUs(){
        header("Access-Control-Allow-Origin: *");
        $about = Db::table('up_other')
            ->field('other_phone, other_email, other_link, other_app_desc, other_one_word, other_app_share')
            ->where('other_id', 4)
            ->find();
        return json(['status' => 200, 'msg' => $about]);
    }
    
    /**
     * 平台说明
     * @return \think\response\Json
     */
    public function usExplain(){
        $explain = Db::table('up_other')->where('other_id', 5)->value('other_explain');
        return json(['status' => 200, 'msg' => $explain]);
    }
    
    /**
     * 意见反馈
     * @return \think\response\Json
     * feedback_content     反馈内容
     * feedback_phone       联系方式
     */
    public function feedback(){
        $feedback_content = trim(input('feedback_content'), ' ');
        $feedback_phone = trim(input('feedback_phone'));
        $insert = [
            'feedback_user_id' => $this->user_id,
            'feedback_content' => $feedback_content,
            'feedback_phone' => $feedback_phone,
            'feedback_time' => $this->create_time
        ];
        $res = Db::table('up_feedback')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '提交成功']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 常见问题分类
     * @return \think\response\Json
     */
    public function problemType(){
        $cate = Db::table('up_problem_type')->field('pt_id, pt_name')->order('pt_time desc')->select();
        if($cate){
            return json(['status' => 200, 'msg' => $cate]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
        
    }
    
    /**
     * 常见问题列表
     * @return \think\response\Json
     * cate_id    问题分类ID
     */
    public function problemList(){
        $cate_id = input('cate_id');
        $problem = Db::table('up_problem')
            ->where('problem_type_id', $cate_id)
            ->field('problem_id, problem_title')
            ->page($this->page, 10)
            ->order('problem_time desc')
            ->select();
        if($problem){
            return json(['status' => 200, 'msg' => $problem]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 问题详情
     * @return \think\response\Json
     * problem_id     问题ID
     */
    public function problemDetail(){
        $problem_id = input('problem_id');
        $problem = Db::table('up_problem')
            ->where('problem_id', $problem_id)
            ->field('problem_title, problem_content')
            ->find();
        return json(['status' => 200, 'msg' => $problem]);
    }
    
    /**
     * 风讯回显
     * @return \think\response\Json
     * info_id   风讯ID
     */
    public function showInfo(){
        $info_id = input('info_id');
        $info = Db::table('up_information')
            ->field('info_content, info_images, info_type, info_addr, info_show, info_friend_user, 
            info_lon, info_lat')
            ->where('info_id', $info_id)
            ->find();
        if($info['info_type'] == 2){ // 图片处理
            $img = trim($info['info_images'], ',');
            $info['info_images'] = explode(',', $img);
        }
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 编辑风讯
     * @return \think\response\Json
     * info_show     可见人群   1 公开  2 粉丝  3 好友  4 本人可见
     * info_friend_user    可见的好友
     * info_is_publish    1 草稿箱   2 发布
     * info_content   发布内容
     * info_images    图片/视频
     * info_type   1 文字  2 图片文字   3 视频文字
     * info_addr      发布的地址
     * info_lon       经度
     * info_lat       纬度
     * info_id       风讯ID
     */
    public function updateInfo(){
        $params = input('');
        $update = [
//            'info_show' => $params['info_show'],
            'info_is_publish' => $params['info_is_publish'],
            'info_time' => $this->create_time
        ];
        if(isset($params['info_content']) && !empty($params['info_content'])){
            $update['info_content'] = $params['info_content'];
        }
        if(isset($params['info_images']) && !empty($params['info_images'])){
            $update['info_images'] = $params['info_images'];
        }
        if(isset($params['info_type']) && !empty($params['info_type'])){
            $update['info_type'] = $params['info_type'];
        }
        if(isset($params['info_show']) && !empty($params['info_show'])){
            $update['info_show'] = $params['info_show'];
        }
        if(isset($params['info_addr']) && !empty($params['info_addr'])){
            $update['info_addr'] = $params['info_addr'];
            $update['info_lon'] = $params['info_lon'];
            $update['info_lat'] = $params['info_lat'];
        }
        if($params['info_show'] == 3){
            $update['info_friend_user'] = ',' . $params['info_friend_user'] . ',';
        }
        $res = Db::table('up_information')->where('info_id', $params['info_id'])->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '编辑成功']);
        }else{
            return json(['status' => 0, 'msg' => '编辑失败']);
        }
    }
        
    /**
     * 我参加的赛事
     * @param $status    0 全部   1 未开始  2 进行中  3 已结束
     * @param $match_cate     0  全部    1 帆船    2 赛艇
     * @param $type          1  列表    2 统计
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function myMatch($status, $match_cate, $type){
        $where['ma_user_id'] = $this->user_id;
        $where['ma_apply_status'] = 2;
        if($status != 0){
            $where['match_status'] = $status;
        }else{
            $where['match_status'] = ['IN', '1,2,3'];
        }
        if($match_cate != 0){
            $where['match_cate'] = $match_cate;
        }
        $data = Db::table('up_match_apply_info')
            ->join('up_match', 'match_id = ma_match_id', 'left');
        if($type == 1){
          $data->join('up_area', 'district_id = match_shi', 'left')
                ->field('match_id, match_title, match_image, match_country_name, district, match_cate, match_status')
                ->order('ma_time desc')
                ->page($this->page, 10);
        }
        $data = $data->where($where)->select();
        return $data;
    }
    
    /**
     * 粉丝
     * @param $user_id    我的/其他用户的ID
     * @param $range      1 查看列表    2 统计数量   3 粉丝ID数组
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function myFans($user_id, $range){
        $fans = Db::table('up_like')
            ->join('up_u_user', 'like_user_id = user_id', 'left')
            ->where('like_by_user_id', $user_id)
//            ->where('user_status', 1)
            ->field('user_id, user_username, user_image, user_intro');
        if($range == 1){ // 列表
            $fans->page($this->page, 10)
                ->order('like_time desc');
        }
        if($range == 3){
            $fans = $fans->column('user_id');
        }else{
            $fans = $fans->select();
        }
        return $fans;
    }
    
    /**
     * 省
     * @return \think\response\Json
     */
    public function getProvince(){
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        return json(['status' => 200, 'msg' => $province]);
    }
    
    /**
     * 市
     * @return \think\response\Json
     * province_id   省ID
     */
    public function getCity($province_id){
        $city = Db::table('up_area')->where('pid', $province_id)->field('district_id, district')->select();
        return json(['status' => 200, 'msg' => $city]);
    }
    
    /**
     * 区
     * @param $city_id   城市ID
     * @return \think\response\Json
     */
    public function getDistrict($city_id){
        $district = Db::table('up_area')->where('pid', $city_id)->field('district_id, district')->select();
        return json(['status' => 200, 'msg' => $district]);
    }
    
    /**
     * 国际国家列表
     * @return \think\response\Json
     */
    public function interCountry(){
        $list = Db::table('up_area_in')
            ->where('level', 1)
            ->field('district_id, district')
            ->select();
        return json(['status' => 200, 'msg' => $list]);
    }
    
    /**
     * 国际州/城市
     * @param $country_id    国家ID
     * @return \think\response\Json
     */
    public function interProvince($country_id){
        $list = Db::table('up_area_in')
            ->where('pid', $country_id)
            ->field('district_id, district')
            ->select();
        return json(['status' => 200, 'msg' => $list]);
    }
    
    /**
     * 国际城市
     * @param $province_id    州ID
     * @return \think\response\Json
     */
    public function interCity($province_id){
        $list = Db::table('up_area_in')
            ->where('pid', $province_id)
            ->field('district_id, district')
            ->select();
        return json(['status' => 200, 'msg' => $list]);
    }
    
    /**
     * 保存在线文件
     * @return \think\response\Json
     * file_url    在线文件地址
     * file_name   文件名称
     * file_bmp    文件格式
     * file_size   文件大小
     */
    public function saveFile(){
        $file_url = input('file_url');
        $of_name = input('file_name');
        $of_bmp = input('file_bmp');
        $of_size = input('file_size');
        $insert = [
            'of_user_id' => $this->user_id,
            'of_url' => $file_url,
            'of_time' => $this->create_time,
            'of_name' => $of_name,
            'of_bmp' => $of_bmp,
            'of_size' => $of_size
        ];
        $res = Db::table('up_online_files')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '保存成功']);
        }else{
            return json(['status' => 0, 'msg' => '保存失败']);
        }
    }
    
    /**
     * 我的文件
     * @return \think\response\Json
     */
    public function myFiles(){
        $list = Db::table('up_online_files')
            ->where('of_user_id', $this->user_id)
            ->field('of_id, of_name, of_bmp, of_url, of_size')
            ->order('of_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 关注
     * @param $user_id    我的/其他用户的ID
     * @param $range      1 查看列表    2 统计数量   3 关注的用户ID数组
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function myLike($user_id, $range){
        $like = Db::table('up_like')
            ->join('up_u_user', 'like_by_user_id = user_id', 'left')
            ->where('like_user_id', $user_id)
//            ->where('user_status', 1)
            ->field('user_id, user_username, user_image, user_intro');
        if($range == 1){ // 列表
            $like->page($this->page, 10)
                ->order('like_time desc');
        }
        if($range == 3){
            $like = $like->column('user_id');
        }else{
            $like = $like->select();
        }
        return $like;
    }
    
    /**
     * 用户主页的风讯
     * @param $user_id    自己/用户的ID或关注的人的ID
     * @param $type       1 用户发布    2 关注的用户的发布
     * @param $publish    1 草稿箱  2 发布    关注的人或其他用户传2
     * @return false|\PDOStatement|string|\think\Collection
     */
    private function infomation($user_id, $type, $publish){
        $in = '1';
        // 我是否是该用户的好友
//        $friend = Db::table('up_friend')
//            ->where('f_status', 2)
//            ->where(function ($query) use($user_id){
//                $query->where(['f_my_id' => $this->user_id, 'f_friend_id' => $user_id])
//                    ->whereOr(['f_friend_id' => $this->user_id, 'f_my_id' => $user_id]);
//            })
//            ->select();
//        if($friend){
//            $in = $in . ',3';
//        }
        // 我是否是该用户的粉丝
        $fans = Db::table('up_like')
            ->where('like_by_user_id', 'IN', $user_id)
            ->column('like_user_id');
        if(in_array($this->user_id, $fans)){
            $in = $in . ',2';
        }
//        $where['info_is_publish'] = $publish;
        $where = 'info_is_publish = ' . $publish . ' and info_status = 1';
        if($user_id == $this->user_id){ // 我自己的主页
//            $where['info_user_id'] = $user_id;
            $where .= ' and info_user_id = ' . $user_id;
        }else{
            $my_suer_id = '%,' . $this->user_id . ',%';
            $where .= ' and (info_show in (' . $in . ') or (info_show = 3 and info_friend_user like "' . $my_suer_id . '"))';
//            $where['info_show'] = [['IN', $in], [['=', 3], ['like', '%,' . $this->user_id . ',%'], 'and'], 'or'];
            if($type == 1){ // 其他用户发布
//                $where['info_user_id'] = $user_id;
                $where .= ' and info_user_id = ' . $user_id;
            }else{
//                $where['info_user_id'] = ['IN', $user_id];
                $where .= ' and info_user_id in (' . implode(',', $user_id) . ')';
            }
        }
        $info = Db::table('up_information')
            ->join('up_u_user', 'user_id = info_user_id', 'left')
            ->field('user_id, user_username, user_image, info_id, info_content, info_images, 
            info_addr, info_time, info_type, info_read_num, info_link')
            ->where($where)
            ->page($this->page, 10)
            ->order('info_time desc')
            ->select();
        return $info;
    }
}